package com.codingfuture.erp_system.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.HashMap;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    /**
     * 结果类
     */
    private int code;
    private String msg;
    private T data;
    private static final int DEFAULT_SUCCESS_CODE = 200;
    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功！";

    public static Result<Void> ok() {
        Result<Void> result = new Result<>();
        result.code = DEFAULT_SUCCESS_CODE;
        result.msg = DEFAULT_SUCCESS_MESSAGE;
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.code = DEFAULT_SUCCESS_CODE;
        result.data = data;
        result.msg = DEFAULT_SUCCESS_MESSAGE;
        return result;
    }

    public static Result<Void> error(int code, String msg) {
        Result<Void> result = new Result<>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static <T> Result<HashMap<Object, Object>> OK(T data) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows", data);
        return Result.ok(map);
    }
}

